layui.use(['table', 'laydate'], function () {
    let layPage = layui.laypage; //分页
    let table = layui.table; //表格
    let form = layui.form; //表单
    let layer = layui.layer; //弹层
    let layDate = layui.laydate; //
    let $ = layui.jquery;

    let jobType = sessionStorage.getItem("type");

    function reloadAndClose(reload, close) {
        table.reload(reload);
        if (close != null) {
            layer.close(close);
        }
    }

    layDate.render({
        elem: '#ageOfConstruction',
        type: 'year'
    });

    layDate.render({
        elem: '#ageOfConstruction1',
        type: 'year'
    });

    table.render({
        elem: '#aircraftCarrier',
        url: '/aircraftCarrier/findAircraftCarrier',
        page: true,
        method: 'get',
        cols: [
            [
                {
                    field: 'portNumber',
                    title: "舷号",
                    align: 'center',
                    sort: true
                },
                {
                    field: 'name',
                    title: "名称",
                    align: 'center'
                },
                {
                    field: 'overallLength',
                    title: "全长",
                    align: 'center'
                },

                {
                    field: 'ageOfConstruction',
                    title: '建造年代',
                    align: "center"
                },
                {
                    field: 'displacement',
                    title: "排水量",
                    align: 'center'
                },
                {
                    field: 'rank',
                    title: '型号',
                    align: 'center'
                },
                {
                    field: 'draught',
                    title: '吃水深度',
                    align: 'center'
                },
                {
                    field: 'carrierSpeed',
                    title: "航速",
                    align: 'center'
                },
                {
                    field: 'endurance',
                    title: "续航力",
                    align: 'center'
                },
                {
                    field: 'power',
                    title: "功率",
                    align: 'center'
                },
                {
                    field: 'homePort',
                    title: "母港",
                    align: 'center'
                },
                {
                    field: 'missile',
                    title: "搭载导弹",
                    align: 'center'
                },
                {
                    field: 'gun',
                    title: "搭载火炮",
                    align: 'center'
                },
                {
                    field: 'radar',
                    title: "雷达",
                    align: 'center'
                },
                {
                    field: 'commander',
                    title: "指挥员",
                    align: 'center'
                },
                {
                    title: '操作',
                    align: 'center',
                    sort: false,
                    toolbar: '#barEdit'
                }
            ]
        ],
        limits: [1, 3, 5, 7, 9],
        limit: 3,
        done: function (res, curr, count) {
            $("[lay-id='aircraftCarrier']>.layui-table-page").css({
                "text-align": "center"
            });
        }
    });

    let updatePage = null;
    let aircraftCarrierId = 0;
    table.on('tool(pageTable)', function (obj) {
        let data = obj.data,
            layEvent = obj.event;
        switch (layEvent) {
            case 'del':
                layer.confirm('确定要删除吗', function (index) {
                    if (index != 0) {
                        $.get('/aircraftCarrier/deleteById', {"id": data.aircraftCarrierId}, (result) => {
                            if (result.state == 200) {
                                layer.msg(result.message, {
                                    icon: 1,
                                    time: 3000
                                })
                                reloadAndClose('aircraftCarrier', index);
                            } else {
                                layer.msg(result.message, {
                                    icon: 2,
                                    time: 3000
                                });
                                reloadAndClose('aircraftCarrier', index);
                            }
                        });
                    }
                });
                break;
            case 'edit':
                aircraftCarrierId = data.aircraftCarrierId;
                $("input[name=portNumber]").val(data.portNumber);
                $("input[name=name]").val(data.name);
                $("input[name=overallLength]").val(data.overallLength);
                $("input[name=ageOfConstruction]").val(data.ageOfConstruction);
                $("input[name=displacement]").val(data.displacement);
                $("input[name=rank]").val(data.rank);
                $("input[name=draught]").val(data.draught);
                $("input[name=carrierSpeed]").val(data.carrierSpeed);
                $("input[name=endurance]").val(data.endurance);
                $("input[name=power]").val(data.power);
                $("input[name=missile]").val(data.missile);
                $("input[name=homePort]").val(data.homePort);
                $("input[name=gun]").val(data.gun);
                $("input[name=radar]").val(data.radar);
                $("input[name=commander]").val(data.commander);
                updatePage = layer.open({
                    type: 1,
                    title: '修改信息',
                    content: $("#updateAircraftCarrierInfo"),
                    area: ['600px', '500px'],
                    shadeClose: false,
                    end: function () {
                        $("#updateAircraftCarrierInfo").hide();
                    }
                });
                break;
        }
    });


    let openAdd = null;
    $('#add').click(() => {
        $("#addAircraftCarrier")[0].reset();
        openAdd = layer.open({
            type: 1,
            title: '添加信息',
            content: $("#createAircraftCarrier"),
            area: ['600px', '500px'],
            shadeClose: false,
            end: function () {
                $("#createAircraftCarrier").hide();
            }
        });
    });

    form.on('submit(addAircraftCarrier)', function (data) {
        let addAircraftCarrier = $('#addAircraftCarrier').serialize();
        $.post('/aircraftCarrier/addAircraftCarrier', addAircraftCarrier, (result) => {
            if (result.state == 200) {
                layer.msg(result.message, {
                    icon: 1,
                    time: 3000
                });
                reloadAndClose('aircraftCarrier', openAdd);
            } else {
                layer.msg(result.message, {
                    icon: 2,
                    time: 3000
                });
                reloadAndClose('aircraftCarrier', null);
            }
        });
        return false;
    });

    form.on('submit(updateAircraftCarrier)', function (data) {
        let updateAircraftCarrier = $('#updateAircraftCarrier').serialize();
        $.post('/aircraftCarrier/updateAircraftCarrier?aircraftCarrierId=' + aircraftCarrierId, updateAircraftCarrier, (result) => {
            if (result.state == 200) {
                layer.msg(result.message, {
                    icon: 1,
                    time: 3000
                });
                reloadAndClose('aircraftCarrier', updatePage);
            } else {
                layer.msg(result.message, {
                    icon: 2,
                    time: 3000
                });
                reloadAndClose('aircraftCarrier', null);
            }
        })
        return false;
    });
});